% 主函数
function main()
    % 问题参数设置
    n = 40;                   % 物品数量
    capacity = 80;            % 背包容量
    weights = randi([1, 10], 1, n);   % 物品重量
    values = randi([10, 30], 1, n);   % 物品价值
    
    max_iter = 100;           % 最大迭代次数
    
    % 运行四种算法
    [pso_best_val, pso_iter_vals] = pso(weights, values, capacity, max_iter);
    [ga_best_val, ga_iter_vals] = ga(weights, values, capacity, max_iter);
    [greedy_best_val, greedy_iter_vals] = greedy(weights, values, capacity);
    [hybrid_best_val, hybrid_iter_vals] = hybrid(weights, values, capacity, max_iter);
    
    % 显示结果
    fprintf('PSO算法最优值: %d\n', pso_best_val);
    fprintf('GA算法最优值: %d\n', ga_best_val);
    fprintf('贪心算法最优值: %d\n', greedy_best_val);
    fprintf('混合算法最优值: %d\n', hybrid_best_val);
    
    % 绘制学习曲线
    figure;
    plot(1:length(pso_iter_vals), pso_iter_vals, 'r-', 'LineWidth', 2);
    hold on;
    plot(1:length(ga_iter_vals), ga_iter_vals, 'b-', 'LineWidth', 2);
    plot(1:length(greedy_iter_vals), greedy_iter_vals, 'g-', 'LineWidth', 2);
    plot(1:length(hybrid_iter_vals), hybrid_iter_vals, 'k-', 'LineWidth', 2);
    legend('粒子群算法', '遗传算法', '贪心算法', '混合算法');
    xlabel('迭代次数');
    ylabel('最优值');
    title('算法迭代学习曲线对比');
    grid on;
end 